/**
协程 占用内存的分配
*/
package main

import (
	"fmt"
	"runtime"
	"sync"
)

func getGoroutineMemConsume() {
	var c chan int
	var wg sync.WaitGroup
	const groutineNum = 1*10 ^ 4

	memConsumed := func() uint64 {
		runtime.GC()
		var memStat runtime.MemStats
		runtime.ReadMemStats(&memStat)
		return memStat.Sys
	}

	noop := func() {
		wg.Done()
		<-c
	}

	wg.Add(groutineNum)
	before := memConsumed()
	for i := 0; i < groutineNum; i++ {
		go noop()
	}

	wg.Wait()
	after := memConsumed()
	fmt.Printf("groutineNum %d\n", groutineNum)
	fmt.Printf("%.3f KB\n", float64(after-before)/groutineNum/1000)
}

func main() {
	getGoroutineMemConsume()
}
